/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;

/* @author Benjamin 
 * Giver let adgang til databasen.
 */
public class DatabaseConnection {

    private static DatabaseConnection instance;
    private Connection connection;

    public void connect() throws FileNotFoundException, SQLException {
        if (connection == null) {
            File connectionFile = new File("connection.txt");
            Scanner scan = new Scanner(connectionFile);

            String path = scan.nextLine();
            String uName = scan.nextLine();
            String uPassword = "";
            if(scan.hasNext()){
                uPassword = scan.nextLine();
            }else{
                uPassword = "";
            }

            connection = DriverManager.getConnection(path, uName, uPassword);

            scan.close();
        }
    }

    public Connection getConnection() throws SQLException, FileNotFoundException {
        if (connection == null) {
            File connectionFile = new File("connection.txt");
            Scanner scan = new Scanner(connectionFile);

            String path = scan.nextLine();
            String uName = scan.nextLine();
            String uPassword = scan.nextLine();

            connection = DriverManager.getConnection(path, uName, uPassword);

            scan.close();
        }
        return connection;
    }

    public static DatabaseConnection getInstance() {
        if (instance == null) {
            instance = new DatabaseConnection();
        }
        return instance;
    }
}
